package com.xuerhua.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.common.enums.TeacherChangeCourseApply;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.TeacherExchangeCourseApplyItemReqVO;
import com.xuerhua.yxt.reqVo.TeacherExchangeCourseApplyReqVO;
import com.xuerhua.yxt.service.IOrganizeGroupService;
import com.xuerhua.yxt.service.ITeacherChangeCourseApplyService;
import com.xuerhua.yxt.view.TeacherExchangeCourseApplyItemView;
import com.xuerhua.yxt.view.TeacherExchangeCourseApplyView;
import com.xuerhua.yxt.vo.TeacherChangeCourseApplyQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.TEACHER_CHANGE_COURSE)
@Controller
public class TeacherChangeCourseController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(TeacherChangeCourseController.class);
	
	@Autowired
	private ITeacherChangeCourseApplyService teacherChangeCourseApplyService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;

	@RequestMapping(value={"","/"})
	public String listPage(Model model) {
		AppUser appUser = getAppUser();
		int count = organizeGroupService.isCourseApprovalGroup(appUser.getTeacher().getId());
		int isAuditer = 0;
		if (count > 0) {
			isAuditer = 1;
		}
		model.addAttribute("isAuditer", isAuditer);
		return "teacherChangeCourse/list";
	}
	
	@RequestMapping("publish")
	public String publishPage() {
		return "teacherChangeCourse/publish";
	}
	
	@RequestMapping("detail/{applyId}")
	public String queryByApplyId(@PathVariable Integer applyId, Model model) {
		AppUser appUser = getAppUser();
		TeacherExchangeCourseApplyView detail = teacherChangeCourseApplyService.selectViewById(applyId);
		model.addAttribute("detail", detail);
		int isAuditer = 0;
		int count = organizeGroupService.isCourseApprovalGroup(appUser.getTeacher().getId());
		if(count > 0) {
			isAuditer = 1;
		}
		model.addAttribute("isAuditer", isAuditer);
		return "teacherChangeCourse/detail";
	}
	
	@RequestMapping("itemDetail/{itemId}")
	public String queryByItemId(@PathVariable Integer itemId, Model model) {
		TeacherExchangeCourseApplyItemView detail = teacherChangeCourseApplyService.selectByItemId(itemId);
		model.addAttribute("detail", detail);
		return "teacherChangeCourse/itemDetail";
	}
	/**
	 * 审核中
	 * @param vo
	 * @return
	 */
	@RequestMapping("pending/page")
	@ResponseBody Object pending(TeacherChangeCourseApplyQueryVO vo) {
		try {
			Page<TeacherExchangeCourseApplyView> page = new Page<TeacherExchangeCourseApplyView>();
			Grid grid = new Grid();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("UPDATE_TIME", "DESC"));
			sorts.add(new OrderBy("CREATE_TIME", "DESC"));
			page.setSorts(sorts);
			List<TeacherExchangeCourseApplyView> list = teacherChangeCourseApplyService.selectPendingListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--pending error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 已驳回
	 * @param vo
	 * @return
	 */
	@RequestMapping("reject/page")
	@ResponseBody Object process(TeacherChangeCourseApplyQueryVO vo) {
		try {
			Page<TeacherExchangeCourseApplyView> page = new Page<TeacherExchangeCourseApplyView>();
			Grid grid = new Grid();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("UPDATE_TIME", "DESC"));
			page.setSorts(sorts);
			List<TeacherExchangeCourseApplyView> list = teacherChangeCourseApplyService.selectRejectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--process error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 已同意
	 * @param vo
	 * @return
	 */
	@RequestMapping("agree/page")
	@ResponseBody Object agree(TeacherChangeCourseApplyQueryVO vo) {
		try {
			Page<TeacherExchangeCourseApplyView> page = new Page<TeacherExchangeCourseApplyView>();
			Grid grid = new Grid();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("UPDATE_TIME", "DESC"));
			page.setSorts(sorts);
			List<TeacherExchangeCourseApplyView> list = teacherChangeCourseApplyService.selectAgreeListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--audited error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("item/page")
	@ResponseBody Object itemPage(TeacherChangeCourseApplyQueryVO vo) {
		try {
			Page<TeacherExchangeCourseApplyItemView> page = new Page<>();
			Grid grid = new Grid();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("UPDATE_TIME", "DESC"));
			page.setSorts(sorts);
			List<TeacherExchangeCourseApplyItemView> list = teacherChangeCourseApplyService.selectItemListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--itemPage error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("publish/submit")
	@ResponseBody Object publishSubmit(@RequestBody TeacherExchangeCourseApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
			reqVO.setTermId(getCurrentTerm().getId());
			reqVO.setApplyTeacherId(appUser.getTeacher().getId());
			reqVO.setApplyTeacherName(appUser.getTeacher().getName());
			int count = 0;
			for(TeacherExchangeCourseApplyItemReqVO itemReqVO : reqVO.getList()) {
				if(itemReqVO.getHasExchangeTeacher() == 1) {
					itemReqVO.setStatus(com.xuerhua.yxt.common.enums.TeacherChangeCourseApply.PENDING.getCode());
					count += 1;
				}else {
					itemReqVO.setStatus(com.xuerhua.yxt.common.enums.TeacherChangeCourseApply.AUDIT_TEACHER_ING.getCode());
				}
			}
			reqVO.setStatus(count > 0 ? com.xuerhua.yxt.common.enums.TeacherChangeCourseApply.PENDING.getCode() : 
				com.xuerhua.yxt.common.enums.TeacherChangeCourseApply.AUDIT_TEACHER_ING.getCode());
			teacherChangeCourseApplyService.addApply(reqVO, appUser.getTeacher());
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--publishSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("audit/success")
	@ResponseBody Object auditSuccess(@RequestBody TeacherExchangeCourseApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setStatus(TeacherChangeCourseApply.AGREE.getCode());
			return teacherChangeCourseApplyService.updateAudit(
					reqVO, appUser.getTeacher());
		} catch (Exception e) {
			logger.error("--auditSuccess error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("audit/reject")
	@ResponseBody Object auditReject(TeacherExchangeCourseApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setStatus(TeacherChangeCourseApply.AUDIT_TEACHER_NO.getCode());
			return teacherChangeCourseApplyService.updateAudit(reqVO, 
					appUser.getTeacher());
		} catch (Exception e) {
			logger.error("--auditReject error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("audit/cancel")
	@ResponseBody Object auditCancel(TeacherExchangeCourseApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setStatus(TeacherChangeCourseApply.AUDIT_TEACHER_CANCEL.getCode());
			return teacherChangeCourseApplyService.updateAudit(reqVO, 
					appUser.getTeacher());
		} catch (Exception e) {
			logger.error("--auditCancel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("teacher/audit/success")
	@ResponseBody Object teacherAuditSuccess(TeacherExchangeCourseApplyItemReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setStatus(TeacherChangeCourseApply.EXCHANGE_TEACHER_OK.getCode());
			return teacherChangeCourseApplyService.updateTeacherAudit(reqVO, appUser.getTeacher());
		} catch (Exception e) {
			logger.error("--teacherAuditSuccess error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("teacher/audit/reject")
	@ResponseBody Object teacherAuditReject(TeacherExchangeCourseApplyItemReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setStatus(TeacherChangeCourseApply.EXCHANGE_TEACHER_NO.getCode());
			return teacherChangeCourseApplyService.updateTeacherAudit(reqVO, appUser.getTeacher());
		} catch (Exception e) {
			logger.error("--teacherAuditSuccess error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
